package com.lenovo.smarttraffic.ui.activity.stage4;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lenovo.smarttraffic.MainActivity;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.SubwayInfoBean;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.HttpUtils;
import com.lenovo.smarttraffic.util.Utils;

import java.util.List;

public class RealTrafficActivity extends BaseActivity {

    public static List<SubwayInfoBean.ROWSDETAILBean> beanList;

    private Toolbar toolbar;
    private TextView toolbar_title;

    private ExpandableListView listView;

    private ScrollView road_find;
    private TextView pos_tv, road_tv, money_tv, start_road, start_time, end_time, end_road, start_time2, end_time2;
    private LinearLayout road_parent;

    @Override
    protected int getLayout() {
        return R.layout.activity_real_traffic;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        toolbar = findViewById(R.id.toolbar);
        toolbar_title = findViewById(R.id.toolbar_title);
        toolbar_title.setText("实时交通");
        toolbar.setNavigationOnClickListener(v -> {
            if (listView.getVisibility() == View.GONE) {
                toggleView();
            } else finish();
        });

        listView = findViewById(R.id.ex_listView);

        pos_tv = findViewById(R.id.pos_tv);
        road_find = findViewById(R.id.road_find);
        road_tv = findViewById(R.id.road_tv);
        money_tv = findViewById(R.id.money_tv);

        start_road = findViewById(R.id.start_road);
        start_time = findViewById(R.id.start_time);
        end_time = findViewById(R.id.end_time);

        end_road = findViewById(R.id.end_road);
        start_time2 = findViewById(R.id.start_time2);
        end_time2 = findViewById(R.id.end_time2);

        road_parent = findViewById(R.id.road_parent);

        initData();
    }

    private void initData() {
        HttpUtils.post("get_metro", new HttpUtils().setParam("UserName", MainActivity.userName),
                response -> {
                    SubwayInfoBean subwayInfoBean = new Gson().fromJson(response.toString(), SubwayInfoBean.class);
                    beanList = subwayInfoBean.getROWS_DETAIL();

                    listView.setAdapter(new RealTrafficAdapter());
                    listView.setOnChildClickListener((parent, v, groupPosition, childPosition, id) -> {
                        ((CheckedTextView) road_parent.getChildAt(groupPosition)).performClick();

                        toggleView();
                        return true;
                    });

                    initView();
                });
    }

    private void initView() {
        //Remove all views before add
        road_parent.removeAllViewsInLayout();
        for (int i = 0; i < beanList.size(); i++) {
            CheckedTextView textView = new CheckedTextView(this);
            textView.setText(beanList.get(i).getStart_place());
            textView.setTextColor(Utils.getColor(this, R.color.Black));
            textView.setTextSize(18);
            textView.setPadding(40, 20, 40, 20);
            textView.setCompoundDrawablesWithIntrinsicBounds(null, getDrawable(R.drawable.select_traffic_bg), null, null);
            textView.setCompoundDrawablePadding(10);
            textView.setBackgroundResource(R.drawable.ripple_round_bg);
            int finalI = i;
            textView.setOnClickListener(v -> {
                //Set data only for unchecked views
                if (!((CheckedTextView) v).isChecked()) {
                    //Single choice
                    for (int j = 0; j < road_parent.getChildCount(); j++) {
                        ((CheckedTextView) road_parent.getChildAt(j)).setChecked(road_parent.getChildAt(j) == v);
                    }

                    SubwayInfoBean.ROWSDETAILBean bean = beanList.get(finalI);

                    pos_tv.setText(bean.getStart_place());
                    road_tv.setText("地铁" + bean.getMetro_code());
                    money_tv.setText("全程票价：" + Utils.getRandom(5, 20) + "元");

                    String start = "开往" + bean.getStart_place() + "方向";
                    String end = "开往" + bean.getEnd_place() + "方向";

                    SpannableString spannableString = new SpannableString(start);
                    StyleSpan styleSpan = new StyleSpan(Typeface.BOLD);
                    spannableString.setSpan(styleSpan, 2, start.length() - 2, SpannableString.SPAN_EXCLUSIVE_INCLUSIVE);

                    start_road.setText(spannableString);
                    start_time.setText(bean.getStart_place_start_time());
                    end_time.setText(bean.getStart_place_end_time());

                    SpannableString spannableString2 = new SpannableString(end);
                    spannableString2.setSpan(styleSpan, 2, end.length() - 2, SpannableString.SPAN_EXCLUSIVE_INCLUSIVE);

                    end_road.setText(spannableString2);
                    start_time2.setText(bean.getEnd_place_start_time());
                    end_time2.setText(bean.getEnd_place_end_time());
                }
            });

            road_parent.addView(textView);
        }
    }

    private void toggleView() {
        if (listView.getVisibility() == View.VISIBLE) {
            listView.setVisibility(View.GONE);
            road_find.setVisibility(View.VISIBLE);
            toolbar_title.setText("站点查询");
        } else {
            road_find.setVisibility(View.GONE);
            listView.setVisibility(View.VISIBLE);
            toolbar_title.setText("实时交通");
        }
    }

    @Override
    public void onBackPressedSupport() {
        if (listView.getVisibility() == View.GONE) {
            toggleView();
        } else super.onBackPressedSupport();
    }
}

class RealTrafficAdapter extends BaseExpandableListAdapter {

    private List<SubwayInfoBean.ROWSDETAILBean> list = RealTrafficActivity.beanList;

    public RealTrafficAdapter() {
    }

    @Override
    public int getGroupCount() {
        return list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return 4;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        //分组和子选项是否持有稳定的ID, 就是说底层数据的改变会不会影响到它们
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.exlist_traffic_group, parent, false);
            viewHolder = new GroupViewHolder();
            viewHolder.road_tv = convertView.findViewById(R.id.title);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (GroupViewHolder) convertView.getTag();
        }

        SubwayInfoBean.ROWSDETAILBean bean = list.get(groupPosition);

        viewHolder.road_tv.setText("地铁" + bean.getMetro_code());

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder viewHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.exlist_traffic_child, parent, false);
            viewHolder = new ChildViewHolder();
            viewHolder.name_tv = convertView.findViewById(R.id.name_tv);
            viewHolder.more = convertView.findViewById(R.id.more);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ChildViewHolder) convertView.getTag();
        }

        SubwayInfoBean.ROWSDETAILBean bean = list.get(groupPosition);

        viewHolder.name_tv.setText(childPosition < 2 ? bean.getStart_place() : bean.getEnd_place());
        viewHolder.name_tv.setVisibility(isLastChild ? View.GONE : View.VISIBLE);
        viewHolder.more.setVisibility(isLastChild ? View.VISIBLE : View.GONE);

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        //指定位置上的子元素是否可选中
        return true;
    }

    static class GroupViewHolder {
        TextView road_tv;
    }

    static class ChildViewHolder {
        TextView name_tv;
        View more;
    }
}
